
// Copyright (C) Recursion Company. All rights reserved.

#ifndef __MFC_UI_COLOR_PICKER_H__
#define __MFC_UI_COLOR_PICKER_H__

#define MWM_SELCHANGE     (_MW_MSG_FIRST + 200)
#define MWM_SELENDOK      (_MW_MSG_FIRST + 201)
#define MWM_SELENDCANCEL  (_MW_MSG_FIRST + 202)
#define MWM_CLOSEUP       (_MW_MSG_FIRST + 203)
#define MWM_DROPDOWN      (_MW_MSG_FIRST + 204)

typedef struct
{
    const TCHAR* szName;
    COLORREF crColour;
}
ColourTableEntry;

class CColourPopup : public CWnd
{
public:
    inline_ CColourPopup ()
    {
        init ();
    }

    CColourPopup (INT x, INT y, COLORREF crInitialColour, CWnd* pParentWnd, const TCHAR* szDefaultColorName,
            COLORREF clrDefault, const TCHAR* szCustomColorName);
    ~CColourPopup ();

public:
    BOOL Create (INT x, INT y, COLORREF crInitialColour, CWnd* pParentWnd, const TCHAR* szDefaultColorName,
            COLORREF clrDefault, const TCHAR* szCustomColorName);

    //{{AFX_VIRTUAL(CColourPopup)
    virtual LRESULT WindowProc (UINT message, WPARAM wParam, LPARAM lParam);
    virtual void PostNcDestroy ();
    //}}AFX_VIRTUAL

protected:
    void init ();
    BOOL GetCellRect (INT nIndex, CRect& rect);
    void FindCellFromColour (COLORREF crColour);
    void UpdateWindowPos (INT xLeft, INT yTop);
    void CreateToolTips ();
    void ChangeSelection (INT nIndex);
    void EndSelection (INT nMessage);
    void DrawCell (CDC* pDC, INT nIndex);

    inline_ COLORREF GetColour (INT nIndex)  { return m_crColours[nIndex].crColour; }
    inline_ const TCHAR* GetColourName(INT nIndex)  { return m_crColours[nIndex].szName; }
    INT GetIndex (INT row, INT col) const;
    INT GetRow (INT nIndex) const;
    INT GetColumn (INT nIndex) const;

public:
    #define _NUM_COLOUR_TABLE_ENTRIES  32
    static ColourTableEntry m_crColours [];
    static const TCHAR* sGetColorName (COLORREF clr);

protected:
    INT          m_nNumColours;
    INT          m_nNumColumns, m_nNumRows;
    INT          m_nBoxSize, m_nMargin;
    INT          m_nCurrentSel;
    INT          m_nChosenColourSel;
    CString      m_strDefaultColorName;
    COLORREF     m_crDefault;
    CString      m_strCustomColorName;
    CRect        m_rtCustomText, m_rtDefaultText, m_rtWindow;
    CFont        m_objFont;
    CPalette     m_objPalette;
    COLORREF     m_crInitialColour, m_crColour;
    CToolTipCtrl m_objToolTip;
    HWND         m_hParentWnd;
    BOOL         m_blHited;
    BOOL         m_blDlgEntried;

protected:
    //{{AFX_MSG(CColourPopup)
    afx_msg void OnLButtonUp (UINT nFlags, CPoint point);
    afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
    afx_msg void OnPaint ();
    afx_msg void OnMouseMove (UINT nFlags, CPoint point);
    afx_msg void OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg BOOL OnQueryNewPalette ();
    afx_msg void OnPaletteChanged (CWnd* pFocusWnd);
    afx_msg void OnKillFocus (CWnd* pNewWnd);
    afx_msg void OnActivateApp (BOOL bActive, DWORD dwTask);
    afx_msg BOOL OnSetCursor (CWnd* pWnd, UINT nHitTest, UINT message);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

//-------------------------------------------------------------------------------------------

#define CP_MODE_TEXT 1  // edit text colour
#define CP_MODE_BK   2  // edit background colour (default)

class CColourPicker : public CButton
{
// Construction
public:
    CColourPicker();
    DECLARE_DYNCREATE(CColourPicker);

// Attributes
public:
    COLORREF GetColour ();
    void SetColour (COLORREF crColour);

    void SetDefaultText (const TCHAR* szDefaultText);
    void SetCustomText (const TCHAR* szCustomText);

    void SetTrackSelection (BOOL bTracking = TRUE)  { m_bTrackSelection = bTracking; }
    BOOL GetTrackSelection ()  { return m_bTrackSelection; }

    void SetSelectionMode (UINT nMode)  { m_nSelectionMode = nMode; }
    UINT GetSelectionMode ()  { return m_nSelectionMode; };

    void SetBkColour (COLORREF crColourBk);
    COLORREF GetBkColour ()  { return m_crColourBk; }
    
    void SetTextColour (COLORREF crColourText);
    COLORREF GetTextColour ()  { return m_crColourText;}

    void SetWindowSize ();
    
public:
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CColourPicker)
    virtual void DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct);
    virtual void PreSubclassWindow ();
    //}}AFX_VIRTUAL

protected:
    BOOL m_bActive;  // Is the dropdown active?
    BOOL m_bTrackSelection;  // track colour changes?
    COLORREF m_crColourBk;
    COLORREF m_crColourText;
    UINT m_nSelectionMode;
    CRect m_ArrowRect;

public:
    CString m_strDefaultText, m_strCustomText;

protected:
    //{{AFX_MSG(CColourPicker)
	afx_msg void OnLButtonUp (UINT nFlags, CPoint point);
    afx_msg int OnCreate (LPCREATESTRUCT lpCreateStruct);
    afx_msg LRESULT OnSelEndOK (WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnSelEndCancel (WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnSelChange (WPARAM wParam, LPARAM lParam);
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP ()
};

//-------------------------------------------------------------------------------------------

class IColorEventListener : public IBaseEventListener
{
public:
    virtual void OnColorChanged () = 0;
};

class CVWnd_ColorPicker : public CColourPicker
{
    DECLARE_VOL_NATIVE_WND_CLASS (CVWnd_ColorPicker, CColourPicker, IColorEventListener, FALSE)

public:
    COLORREF GetCurrentColor () const;
    void SetCurrentColor (COLORREF clrCurrent);
    void SetAllowTransparent (BOOL_P blpAllowTransparent);
    void SetTransparentCaption (const TCHAR* szCaption);

public:
    //{{AFX_MSG(CVWnd_ColorPicker)
    afx_msg LRESULT OnSelEndOK (WPARAM wParam, LPARAM lParam);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP ()

public:
    BOOL_P m_blpAllowTransparent;
    CVolString m_strTransparentCaption;

protected:
    COLORREF m_clrCurrent;
};


#endif
